//
//  MusicViewController.m
//  CarOfHome2
//
//  Created by dlios on 15/9/23.
//  Copyright (c) 2015年 lanou3g.com 蓝鸥科技. All rights reserved.
//

#import "MusicViewController.h"
#import "HTTPTool.h"
#import "MusicModel.h"
#import "MusicTableViewCell.h"
#import "MusicPlayViewController.h"

@interface MusicViewController ()<UITableViewDataSource,UITableViewDelegate>
@property (nonatomic,strong) UITableView *tableView;
@property (nonatomic,strong) UIImageView *image;
@property (nonatomic,strong) NSMutableArray *valueArr;
@property (nonatomic,strong) MBProgressHUD *HUD;
@end

@implementation MusicViewController
- (void)viewWillAppear:(BOOL)animated{
    AppDelegate *dele = [[UIApplication sharedApplication]delegate];
    if (dele.isNight == 1) {
        self.view.backgroundColor = [UIColor blackColor];
        self.tableView.backgroundColor = [UIColor blackColor];
        [self.tableView reloadData];
    }
    else{
        self.view.backgroundColor = [UIColor whiteColor];
        self.tableView.backgroundColor = [UIColor whiteColor];
        [self.tableView reloadData];
    }
    self.navigationController.navigationBarHidden = YES;
    self.tabBarController.tabBar.hidden = NO;
}

#pragma mark -
- (void)Closed
{
    self.HUD = [[MBProgressHUD alloc] initWithView:self.view];
    [self.view addSubview:self.HUD];
    self.HUD.labelText = @"加载中..";
    [self.HUD show:YES];
    self.view.userInteractionEnabled = NO;
    
}
- (void)Opend
{
    [self.HUD hide:YES afterDelay:2];
    self.view.userInteractionEnabled = YES;
    
}
- (void)viewDidLoad {
    [super viewDidLoad];
    self.valueArr = [NSMutableArray array];
    // Do any additional setup after loading the view from its nib.
    [self tableViewSubView];
    [self Closed];
    [self dateHandle];
}
- (void)dateHandle{
    
       [HTTPTool get:Music body:nil httpResult:JSON success:^(id result) {
        NSDictionary *dic = result;
      
        NSMutableArray *arr = [dic objectForKey:@"songs"];
           
        for (NSDictionary *dic in arr) {
            MusicModel *musicModel = [[MusicModel alloc]init];
            [musicModel setValuesForKeysWithDictionary:dic];
           
            if ([musicModel.url isEqualToString:@""]) {
                
            }
            else{
            [self.valueArr addObject:musicModel];
            }
        }
           [self.tableView reloadData];
           [self Opend];
    } failure:^(NSError *error) {
        
    }];

}
- (void)tableViewSubView{
   
    self.tableView = [[UITableView alloc]initWithFrame:CGRectMake(0, 0, 375 * SAWindth, 667 * SAHeight) style:UITableViewStylePlain];
    [self.view addSubview:self.tableView];
    self.tableView.backgroundColor = [UIColor colorWithRed:251/255.0f green:247/255.0f blue:237/255.0f alpha:1];
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    self.tableView.separatorStyle = UITableViewCellSeparatorStyleSingleLine;
    
    UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width,  215 * SAHeight )];
    
    self.image = [[UIImageView alloc]init];
    self.image.frame = CGRectMake(0, 0, self.view.frame.size.width,  215 * SAHeight );
    self.image.image = [UIImage imageNamed:@"musicbg.jpg"];
    [view addSubview:self.image];
    self.tableView.tableHeaderView = view;

    UIView *emptyLine = [[UIView alloc] init];
    emptyLine.backgroundColor = [UIColor clearColor];
    self.tableView.tableFooterView = emptyLine;

    
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{

    MusicTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"reuse"];
    if (cell == nil) {
        cell = [[MusicTableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"reuse"];
    }
    MusicModel *musicModel = [[MusicModel alloc]init];
    musicModel = self.valueArr[indexPath.row];
    cell.musicModel = musicModel;
    cell.number.text = [NSString stringWithFormat:@"%ld",indexPath.row +1];
    cell.number.font = [UIFont systemFontOfSize:20];
    cell.accessoryType = 1;
    cell.number.textColor = [UIColor blackColor];
    if (indexPath.row <= 2) {
        cell.number.font = [UIFont fontWithName:@"Arial Rounded MT Bold" size:25];
        cell.number.textColor = [UIColor colorWithRed:0 green:180.0/255.0 blue:250.0/255.0 alpha:1];
    }

    
    
    return cell;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{

    NSMutableArray* (^block1)(NSMutableArray *) = ^(NSMutableArray *valueArr){
        NSString *n = [NSString stringWithFormat:@"%ld", indexPath.row];
        NSLog(@"%@",n);
        [valueArr addObject:self.valueArr];
        [valueArr addObject:n];
        return  valueArr;
    };
    MusicPlayViewController *musicPlay = [[MusicPlayViewController alloc]init];
    musicPlay.block = block1;
    [self.navigationController pushViewController:musicPlay animated:YES];


}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    
    return self.valueArr.count;
    
}
#pragma mark - scrollView 图片下拉放大
- (void)scrollViewDidScroll:(UIScrollView *)scrollView
{
    CGFloat yy = self.tableView.contentOffset.y;
    if (yy <= 0) {
        yy = 0 - yy;
        self.image.frame = CGRectMake(-yy, -yy, self.view.frame.size.width + yy + yy, 215 * SAHeight + yy);
    }
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    return 70;
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
